package com.nageoffer.jobtrain.admin.common.config;

import com.nageoffer.jobtrain.admin.common.biz.user.UserContext;
import com.nageoffer.jobtrain.admin.common.constant.RedisCacheConstant;
import feign.RequestInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * OpenFeign服务器调用用户信息传递配置
 */
@Configuration
@RequiredArgsConstructor
public class OpenFeignConfiguration {
    private final StringRedisTemplate stringRedisTemplate;

    /**
     * openfeign 请求拦截器设置header
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            template.header("Authorization",
                    stringRedisTemplate.opsForValue().get(RedisCacheConstant.ADMIN_USER_LOGIN_KEY + UserContext.getAdminUsername()));
        };
    }
}